/**
 * created by kosmo on 06/06/2017.
 */


Dropzone.autoDiscover = false;

window.addEventListener("load", function () {
    FinanceStorageMain.init();
});



const FinanceStorageMain = {

    init: function () {
        FinanceVideoStorage.init();
        StorageDetailBox.init();
    }

};



const FinanceVideoStorage = {

    _boxDiv_:null,


    _detailMessageKey_: "data-detail-key",
    _fileIndex_: 0,
    _msgMap_: {},

    _lockBtn_:null,

    _dropzone_:null,

    _form_:null,

    _dropZoneOptions_:{
        autoDiscover: false,
        maxFilesize: 20971520,
        uploadMultiple: false,
        addRemoveLinks: true,
        dictDefaultMessage: "拖拽或点击以上传",
        dictFallbackMessage: "您的浏览器不支持拖拽上传",
        dictInvalidFileType: "您上传的文件不是 video",
        dictFallbackText: "请使用传统的表单上传 video ,像往常一样",
        dictCancelUpload: "取消上传",
        dictMaxFilesExceeded: "你不能上传更多的文件",
        dictCancelUploadConfirmation: "确定要取消上传吗?",
        dictRemoveFile: "清除",
        //headers:headerObj,
        accept: function (file, done) {

            if (!FinanceVideoStorage.isExpectFile(file)) {
                // 显示错误
                done("不是 video");
            } else if (file.size > 1262485504) {
                // 大小大于 1 G,显示错误 1262485504
                done("excel 大小不超过 1 G");
            } else {
                // 正确,上传
                done();
            }

        },
        init: function () {
            this.on("success", function (file, result) {
                if (result.status === true) {
                    if (file.previewElement) {
                        FinanceVideoStorage.success(file, result);
                    }
                } else {
                    if (file.previewElement) {
                        FinanceVideoStorage.failure(file, result);
                    }
                }
            });
            this.on("error",function (file,errorMsg,xhr) {
                FinanceVideoStorage.error(file,errorMsg,xhr);
            });
            this.on("addedfile", function (file) {
                FinanceVideoStorage.addFile(file);
            });
            this.on("removedfile", function (file) {
                FinanceVideoStorage.removeFile(file);
            });
            this.on("complete", function (file) {
                FinanceVideoStorage.complete(file);
            })
        }
    },
    init: function () {
        this._boxDiv_ = document.querySelector("div.box.videoUpload");
        this._form_ = this._boxDiv_.querySelector("form.video.dropzone");

        //初始化表单事件
        FinanceVideoStorageFormEvent.init(this._boxDiv_,this._form_);

        this._dropzone_ = new Dropzone("form.video.dropzone", this._dropZoneOptions_);

    }, getFileIndex: function () {
        return ++this._fileIndex_;
    }, upload: function () {

    }, isVideo: function (file) {
        return true;
    }, addFile: function (file) {
        file.previewElement.setAttribute(this._detailMessageKey_, this.getFileIndex());

    }, removeFile: function (file) {

    }, complete: function (file) {

    }, success: function (file, result) {
        let _this;

        file.previewElement.setAttribute(this._detailMessageKey_, result.msg);

        //file.previewElement.title = "点击查看详情";
        _this = this;

        file.previewElement.addEventListener("click", function () {
            var msg;
            msg = this.getAttribute(_this._detailMessageKey_);

            //StorageDetailBox.show(file.name, msg);

        });

        file.previewElement.classList.add("dz-success");

    }, failure: function (file, result) {
        var node, _i, _len, msgDivs, _this;
        file.previewElement.classList.add("dz-error");
        file.previewElement.title = "点击查看详情";
        msgDivs = file.previewElement.querySelectorAll("[data-dz-errormessage]");
        for (_i = 0, _len = msgDivs.length; _i < _len; _i++) {
            node = msgDivs[_i];
            node.innerHTML = result.msg;
        }
        file.previewElement.setAttribute(this._detailMessageKey_, result.msg);
        _this = this;

        file.previewElement.addEventListener("click", function () {
            var msg;
            msg = this.getAttribute(_this._detailMessageKey_);

           StorageDetailBox.show(file.name,msg);
        });
    },error:function (file,errorMsg,xhr) {
        var node, _i, _len, msgDivs, _this,msg;
        file.previewElement.classList.add("dz-error");
        file.previewElement.title = "点击查看详情";
        msgDivs = file.previewElement.querySelectorAll("[data-dz-errormessage]");
        for (_i = 0, _len = msgDivs.length; _i < _len; _i++) {
            node = msgDivs[_i];
            node.innerHTML = xhr.status + " : " + errorMsg.error;
        }

        msg = "";
        for(let key in errorMsg){
            msg += key + " ---- " + errorMsg[key] + "<br/>";
        }
        file.previewElement.setAttribute(this._detailMessageKey_,msg);

        _this = this;
        file.previewElement.addEventListener("click", function () {
            var msg;
            msg = this.getAttribute(_this._detailMessageKey_);

            StorageDetailBox.show(file.name,msg);
        });
    }


};

const FinanceVideoStorageFormEvent = {

    _boxDiv_:null,

    _lockBtn_:null,

    _form_:null,

    init :function (boxDiv,form ) {
        let _this;
        this._boxDiv_ = boxDiv;
        this._form_ = form;
        this._lockBtn_ = this._boxDiv_.querySelector("button.btn.lock");
        _this = this;
        this._lockBtn_.addEventListener("click",function () {
            _this.lockBtnClick();
        });
        this._form_.directoryField.addEventListener("input",function () {
            let input;
            input = _this._form_.directoryField;
            _this._form_.directory.value = input.value;
        });
    },lockBtnClick:function () {
        let input;
        input = this._form_.directoryField;
        if(input.hasAttribute("readonly")){
            // 取消锁定
            input.removeAttribute("readonly");
            this._lockBtn_.innerHTML = "锁定目录";
        }else {
            // 锁定
            input.setAttribute("readonly","readonly")
            this._lockBtn_.innerHTML = "取消锁定";
            this._form_.directory.value = input.value;
        }
    }
};

const StorageDetailBox = {

    _box_: null,

    _boxDialog_: null,

    init: function () {
        this._box_ = document.querySelector("div#detailModal");
        this._boxDialog_ = $(this._box_);
        this._boxDialog_.modal({"show": false});
    }, show: function (title, body) {

        this._box_.querySelector(".modal-title").innerHTML = "文件: " + title;
        this._box_.querySelector(".modal-body").innerHTML = body;
        this._boxDialog_.modal("show");
    }

};